package com.hq.ims.data.utils;

import com.hq.utils.util.JsonUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * lichaojie
 *
 * @ClassName HttpClientUtil
 **/
@Slf4j
public class HttpClientUtil {

    public static Map deleteJson(@NonNull String url, Map<String, String> headerMap) {
        Map content = new HashMap();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpDelete httpdelete = new HttpDelete(url);
        httpdelete.setHeader("Content-type", "application/json;charset=utf-8");
        // 自定义的header内容
        if (null != headerMap && headerMap.size() > 0) {
            headerMap.forEach((key, value) -> httpdelete.addHeader(key, value));
        }
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpdelete);
            HttpEntity entity = httpResponse.getEntity();
            String contentStr = EntityUtils.toString(entity, "UTF-8");
            content = JsonUtils.stringToObject(contentStr, Map.class);
        } catch (Exception var14) {
            log.error(var14.getMessage());
        } finally {
            try {
                if (null != httpResponse) {
                    httpResponse.close();
                }
                if (null != httpClient) {
                    httpClient.close();
                }
            } catch (Exception var14) {
                log.error(var14.getMessage());
            }
        }
        return content;
    }


    private static RequestConfig createRequestConfig(int second) {
        int millisecond = second * 1000;
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(millisecond).setSocketTimeout(millisecond).build();
        return requestConfig;
    }

    public static void setHead(HttpRequestBase requestBase, @Nonnull Map<String, String> params) {
        if (params != null) {
            Iterator var2 = params.entrySet().iterator();

            while (var2.hasNext()) {
                Map.Entry<String, String> m = (Map.Entry) var2.next();
                requestBase.addHeader((String) m.getKey(), (String) m.getValue());
            }
        }

    }


    /**
     * GET
     *
     * @param url 请求地址
     * @return
     */
    public static String doGet(String url) {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (ParseException | IOException e) {
            log.error("连接失败" + e.getMessage());
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

}
